#!/bin/bash
 
#-------------------------------------------------------------------------------
# isccommand.sh
# 
# This shell script is invoked from the Inventory Scout data collection tasks
# for the process of copying data from the hard drive to diskette.
#
# Currently all we'll do is copy the data, but 1) we might need to span
# diskettes, and 2) might consider compressing (zip) the data to conserve
# space (which might require multiple diskettes as well).
# 
# Usage: isccommand <input file to copy> <output file>
#
# Return Codes:
# 1 - Error accessing required save update data logging directory
# 2 - Media is write-protected
# 3 - Error copying data to removable media
# 4 - Error mounting the media
# 5 - input file does not exist
# 6 - Error unmounting the media
# 7 - zip error

#---------------------------------------------------------------------------------
# directory and filename which records the Inventory Scout commands
#---------------------------------------------------------------------------------
LOGDIR=/var/hsc/log
LOG=$LOGDIR/isccommand.log
   
LOG_ERROR_LOG=/tmp/isccommand.log

ZIPCMD=/usr/bin/zip

#-------------------------------------------------------------------------------
# the filename of the raw XML output file - '/var/adm/invscout/"+<model type>_<serial number>+".VPD.xml'
#-------------------------------------------------------------------------------
INPUT_FILE=$1

#-------------------------------------------------------------------------------
# original output filename reformatted to 8.3 - '"V"+<serial number>.xml'
#-------------------------------------------------------------------------------
OUTPUT_FILE=$2


# Check if the directory for the log file exists.
if [ ! -d $LOGDIR ]; then
	echo "=================================================================" > $LOG_ERROR_LOG
	echo -e "Inventory Scout command log for `date`." >> $LOG_ERROR_LOG
	echo "Inventory Scout command log directory, <$LOGDIR>, does not exist. Program exiting" >> $LOG_ERROR_LOG
	exit 1
fi

#
# Just in case we have NLS troubles reading system information...
#
LANG=en_US
export LANG

# Start log to record the Inventory Scout command actions.
echo -e "VPD data collection log for `date`.\n" > $LOG
echo -e "parm 1 = $INPUT_FILE, parm 2 = $OUTPUT_FILE" >> $LOG


#
# Determine the remote diskette mount location dynamically since there may be a USB
# attached drive - see discover_devices script for details
CD_DEV=1
FLOPPY_DEV=2
USB_DISK_DEV=3    #includes memory keys
USB_FLOPPY_DEV=4
UNKNOWN_DEV=5
x=`/opt/hsc/bin/discover_devices`

# set delimeter
export IFS=";"
found_fd=0
found_usb=0
prev_elem=""
for cur_elem in $x
do
    echo "cur_elem returned from discovery program is $cur_elem" >> $LOG
    if [ $cur_elem = $FLOPPY_DEV ]; then
        echo "found internal diskette" >> $LOG
        floppy_device=$prev_elem
        found_fd=1
    elif [ $cur_elem = $USB_FLOPPY_DEV ]; then
        echo "found USB diskette" >> $LOG
        usb_device=$prev_elem
        found_usb=1
    fi
    
    prev_elem=$cur_elem
done

MOUNTPOINT=/mnt/floppy
if [ $found_fd -eq "1" ]; then
    # always use internal drive first
    MOUNT_DEV=$floppy_dev
elif [ $found_usb -eq "1" ]; then
    MOUNT_DEV=$usb_device
else
    # no diskette devices
    echo "No diskette device found." >> $LOG
    exit 6
fi

# Check to see if media is already mounted - if so then unmount it
if grep "$MOUNTPOINT" /etc/mtab; then # Media is already mounted
	if ! umount $MOUNTPOINT >> $LOG 2>&1; then
		echo "Couldn't unmount the media, $MOUNTPOINT." >> $LOG
		exit 6
	fi
fi

# Mount the media and check for write-protect
if mount -v $MOUNT_DEV $MOUNTPOINT > mount_output 2>&1; then
	echo "Media successfully mounted at $MOUNTPOINT." >> $LOG
	if grep "write-protected" mount_output; then
		echo "The media is write-protected." >> $LOG
		umount $MOUNTPOINT
		exit 2
	fi
else
	echo "Couldn't mount the media, $MOUNTPOINT." >> $LOG
	exit 4
fi

# cleanup
rm -f mount_output


# Double check one to see if input file really exists
if [ ! -e $INPUT_FILE ]; then
        echo "File, $INPUT_FILE, does not exist on the HSC." >> $LOG
        umount $MOUNTPOINT
        exit 5
fi

#
# Save to floppy
#
# copy the data to floppy - assume it's been formatted
#
# New for Squadrons GA3 - zip the input XML file since the (ASCII) data
# seems to be exceeding the capacity of the diskette. 11/16/04
#
SHORT_ZIPNAME=${OUTPUT_FILE%%.*}
echo "Prior to command execution - output file to be named $SHORT_ZIPNAME.zip" >> $LOG

$ZIPCMD -rv $SHORT_ZIPNAME $INPUT_FILE >> $LOG 2>&1
if [ $? -eq 0 ]; then
    cp -f -v $SHORT_ZIPNAME.zip $MOUNTPOINT/$SHORT_ZIPNAME.zip >> $LOG 2>&1
    if [ $? -ne 0 ]; then
        echo "Error $? occurred while copying the zip'd VPD data file, $SHORT_NAME.zip, to floppy." >> $LOG
        umount $MOUNTPOINT
        rm -f $SHORT_ZIPNAME.zip
        exit 3
    fi
    # remove temp file
    rm -f $SHORT_ZIPNAME.zip
else
    echo "Error $? occurred while zip'ing the input VPD file." >> $LOG
    umount $MOUNTPOINT
    exit 7
fi

# ensure data flushed, bit-o-status
sync
echo ">> VPD data successfully copied to diskette. <<" >> $LOG


# Unmount the target media and log completion
if umount -v $MOUNTPOINT >> $LOG 2>&1; then
	echo "Floppy diskette media unmounted successfully." >> $LOG
	exit 0
else
        # yeah, this is an unexpected error, but the data on diskette is still valid...
        # should we return a "success" then?
	echo "Couldn't unmount the media at mount point $MOUNTPOINT." >> $LOG
	exit 6
fi
